package cate.game.role.farm.herotrain;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDHero;
import cate.common.table.item.staruptask.HeroStarTaskRow;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.res.hero.introduction.StarRewardStatus;
import cate.game.train.hero.msg.StarUpChallengeResultResp;
import easy.java.dev.note.NoteField;

import java.util.HashSet;

public class HeroStarUpFTC extends AbstractFarmFTC<TrainHeroFight> {

  @NoteField("战斗奖励与配置")
  public HeroStarTaskRow taskRow;
  @NoteField("英雄模板信息")
  public HeroBaseRow tpl;
  @NoteField("英雄实体信息")
  public Hero hero;
  @NoteField("任务Id")
  public int taskId;

  public HeroStarUpFTC() {
  }

  public HeroStarUpFTC(Role role, HeroStarTaskRow taskRow, HeroBaseRow tpl, Hero hero, int taskId) {
    setup(role);
    this.taskRow = taskRow;
    this.tpl = tpl;
    this.hero = hero;
    this.taskId = taskId;
  }


  @Override
  protected TrainHeroFight po(Role role) {
    return role.getFarm().trainHeroFight;
  }

  @Override
  public void handleResult(Role role, PlayPO play) {
    super.handleResult(role, play);
    boolean win = play.winner == GDFight.Team.A;

    StarUpChallengeResultResp resultResp = new StarUpChallengeResultResp();
    resultResp.win = win;
    resultResp.taskId = this.taskId;
    resultResp.uid = hero.uid;
    if (win) {
      if (hero.getTpl(role.toPlayBuildContext()).upType == GDHero.UpType.FLY
              && new HashSet<>(hero.starTaskList).size() == tpl.taskMap.size() - 1) {
        if (!role.getRes().heroIntroduction.heroStarGots.containsKey(tpl.protoId)) {
          role.getRes().heroIntroduction.heroStarGots.put(tpl.protoId, new StarRewardStatus());
        }
        StarRewardStatus starRewardStatus = role.getRes().heroIntroduction.heroStarGots.get(tpl.protoId);
        starRewardStatus.initialize(role);
        if (!starRewardStatus.map.containsKey(tpl.star)) {
          MixRes reward = new MixRes(taskRow.rewardStr);
          resultResp.reward.addList(reward);
          reward.add(role, null);
//          handleResult(role);
          starRewardStatus.map.put(tpl.star, true);
          new HeroStarUpSingleUpdateResp(tpl.protoId, tpl.star).sendNow(role);
        }
      }
      handleResult(role);
    }
    role.sendNow(resultResp);
  }

  public void handleResult(Role role) {
    po(role).successStarUpTask(tpl, hero, taskId, null);
  }
}
